#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Event.h>
#pragma unmanaged 
#include <ql\instruments\callabilityschedule.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Instruments {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICallability
	public ref class CCallability : 
            public CEvent,
            public Cephei::QL::Instruments::ICallability
	{
	protected: 
		boost::shared_ptr<QuantLib::Callability>* _ppCallability;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Callability>* _phCallability;
#endif
		Object^ _CallabilityOwner;     // reference to object that manages the storage for this object
	internal:
        CCallability (boost::shared_ptr<QuantLib::Callability>& childNative, Object^ owner);
        CCallability (QuantLib::Callability& childNative, Object^ owner);
        CCallability (CCallability^ copy);
        CCallability (System::Type^ t);
#ifdef STRUCT
        CCallability (QuantLib::Callability childNative);
#endif       
#ifdef HANDLE
		CCallability (QuantLib::Handle<QuantLib::Callability>& childNative, Object^ owner);
		CCallability (QuantLib::Handle<QuantLib::Callability> childNative);
#endif
		virtual ~CCallability ();
		!CCallability ();

	internal:
		QuantLib::Callability& GetReference ();
		boost::shared_ptr<QuantLib::Callability>& GetShared ();
		QuantLib::Callability* GetPointer ();
        void SetCallability (boost::shared_ptr<QuantLib::Callability> native)
        {
            if (_ppCallability != NULL)
                delete _ppCallability;
            _ppCallability = new boost::shared_ptr<QuantLib::Callability> (native);
            SetEvent (boost::dynamic_pointer_cast<QuantLib::Event> (*_ppCallability));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Callability>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property DateTime Date 
        {
		    virtual DateTime get () ;
        }
        property QL::Instruments::Callability::TypeEnum Type 
        {
		    virtual QL::Instruments::Callability::TypeEnum get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICallability Vector interface
	ref class CCallabilityVector : public System::MarshalByRefObject,  
							    public INativeVector<ICallability^>,
							    public INativeVector<Generic::ICell<ICallability^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::Callability>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::Callability>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Callability>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CCallabilityVector ();
#ifdef STRUCT
		CCallabilityVector (std::vector<QuantLib::Callability>& vec, Object^ owner);
		CCallabilityVector (std::vector<QuantLib::Callability> vec);
#endif       
		CCallabilityVector (std::vector<boost::shared_ptr<QuantLib::Callability>>& vec, Object^ owner);
#ifdef HANDLE
		CCallabilityVector (std::vector<QuantLib::Handle<QuantLib::Callability>>& vec, Object^ owner);
#endif
		virtual ~CCallabilityVector ();
		!CCallabilityVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property ICallability^ default[int] 
		{
			virtual ICallability^ get (int index) = INativeVector<ICallability^>::default::get;
			virtual void set (int index, ICallability^ value) = INativeVector<ICallability^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Instruments::ICallability^>^ CItem[int] 
		{
			virtual Generic::ICell<ICallability^>^ get (int index) = INativeVector<Generic::ICell<ICallability^>^>::default::get;
			virtual void set (int index, Generic::ICell<ICallability^>^ value) = INativeVector<Generic::ICell<ICallability^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::Callability>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::Callability>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Callability>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CCallability_Factory : public System::MarshalByRefObject,  public ICallability_Factory
	{
	public:
        /* <summary> Create a hybrid Vector of ICallability, with event notification of changes </summary> */
        virtual Cephei::IVector<ICallability^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of ICallability, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<ICallability^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<ICallability^>^ CreateVector(System::Collections::Generic::IEnumerable<ICallability^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<ICallability^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<ICallability^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Instruments */}
